﻿using Aliyun.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Common.OSS
{
    public class OssManager : IOssManager
    {
        private readonly OssClient _ossClient;
        private readonly string _bucketName;

        public OssManager(string bucketName)
        {
            //var appkey = ConfigurationManager.AppSettings["OssAppKey"];
            //var secret = ConfigurationManager.AppSettings["OssAccessSecret"];
            //var endPoint = ConfigurationManager.AppSettings["OssEndPoint"];

              //LTAIXHWpGeuQ65xd     uvJk2IoE0bIi0N0RDgaWairUyQWaZy 
              //shnb_allen  m12315

            var appkey = "LTAIXHWpGeuQ65xd";
            var secret = "uvJk2IoE0bIi0N0RDgaWairUyQWaZy";
            var endPoint = "http://oss-cn-beijing.aliyuncs.com";




            //var appkey = "LTAIlbO6Co6dAa5R";
            //var secret = "nlmkwo6AeUrd5fT9S9ThuT61emuOe6";
            //var endPoint = "http://oss-cn-beijing.aliyuncs.com";

            _ossClient = new OssClient(endPoint, appkey, secret);
            _bucketName = bucketName;
        }

        #region 单例模式
        private static readonly object SyncObject = new object();
        private static IOssManager _Instance;

        public static IOssManager Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (SyncObject)
                    {
                        //_Instance = new OssManager("avrilbucket");
                        _Instance = new OssManager("shnbbucket");
                    }
                }
                return _Instance;
            }
        }
        #endregion


        #region 实现方法

        /// <summary>
        /// 上传文件流
        /// </summary>
        /// <param name="key">文件名</param>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public bool PutObject(string key, Stream stream)
        {
            var result = _ossClient.PutObject(_bucketName, key, stream);
            if (!String.IsNullOrEmpty(result.ETag))
                return true;
            return false;
        }

        /// <summary>
        /// 删除Object
        /// </summary>
        /// <param name="key">文件名</param>
        public void DeleteObject(string key)
        {
            _ossClient.DeleteObject(_bucketName, key);
        }

        /// <summary>
        /// 获取Obj
        /// </summary>
        /// <param name="key">文件名</param>
        /// <returns></returns>
        public Stream GetObj(string key)
        {
            var o = _ossClient.GetObject(_bucketName, key);
            using (var requestStream = o.Content)
            {
                byte[] buf = new byte[1024];
                var fs = new MemoryStream();
                var len = 0;
                while ((len = requestStream.Read(buf, 0, 1024)) != 0)
                {
                    fs.Write(buf, 0, len);
                }
                fs.Close();
                return fs;
            }
        }

        /// <summary>
        /// 获取文件访问Url
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetUrl(string key)
        {
            const string OssEndPoint = "http://oss-cn-beijing.aliyuncs.com";

            //return "http://" + _bucketName + "." + ConfigurationManager.AppSettings["OssEndPoint"].Replace("http://", "") + "/" + key;
            return "https://" + _bucketName + "." + OssEndPoint.Replace("http://", "") + "/" + key;

        }



        #endregion

    }
}
